package sample.sound;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class SoundProjectActivity extends Activity {
	SimplifiedSound sound1 = null;
	Button butonPlay, butonPause, butonRestart;
	TextView textViewSoundInfo;
	Activity app = null;
	SeekBar seekBarSound, seekBarVolume;
	int UPDATE_SEEKBAR = 1;
	AudioManager audioManager;
	ComponentName mediaButtonComponent;

	private void setVolumeListener() {
		mediaButtonComponent=new ComponentName(
				getPackageName(), MediaButonListener.class.getName());
		audioManager.registerMediaButtonEventReceiver(mediaButtonComponent);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.soundui);
		app = this;
		m_Handler.sendEmptyMessage(UPDATE_SEEKBAR);
		sound1 = new SimplifiedSound(getApplicationContext(), R.raw.androidsesi);

		audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		setVolumeListener();
		
		
		butonPlay = (Button) findViewById(R.id.buttonPlay);
		butonPlay.setSoundEffectsEnabled(false);
		butonPlay.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				try {
					seekBarSound.setEnabled(true);
					m_Handler.removeMessages(UPDATE_SEEKBAR);
					m_Handler.sendEmptyMessage(UPDATE_SEEKBAR);
					sound1.playSound();
				} catch (Exception e) {
					Toast.makeText(app, "Hata : " + e.toString(),
							Toast.LENGTH_LONG).show();
				}
			}
		});

		butonPause = (Button) findViewById(R.id.buttonPause);
		butonPause.setSoundEffectsEnabled(false);
		butonPause.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {

				sound1.pauseSound();
			}
		});

		butonRestart = (Button) findViewById(R.id.buttonRestart);
		butonRestart.setSoundEffectsEnabled(false);
		butonRestart.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				sound1.restartSound();
			}
		});

		seekBarSound = (SeekBar) findViewById(R.id.seekBarTrack);
		seekBarSound.setEnabled(false);
		seekBarSound
				.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

					@Override
					public void onStopTrackingTouch(SeekBar arg0) {
					}

					@Override
					public void onStartTrackingTouch(SeekBar arg0) {
					}

					@Override
					public void onProgressChanged(SeekBar arg0, int arg1,
							boolean arg2) {
						if (arg2) {
							sound1.seekToPercantage(100f * arg1
									/ (arg0.getMax() * 1f));
						}
					}
				});

		textViewSoundInfo = (TextView) findViewById(R.id.textViewSoundInfo);

		seekBarVolume = (SeekBar) findViewById(R.id.seekBarVolume);
		int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		seekBarVolume
				.setProgress((int) (audioManager
						.getStreamVolume(AudioManager.STREAM_MUSIC)
						/ (maxVolume * 1f) * 100));

		// audio.regi

		seekBarVolume
				.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

					@Override
					public void onStopTrackingTouch(SeekBar arg0) {
					}

					@Override
					public void onStartTrackingTouch(SeekBar arg0) {
					}

					@Override
					public void onProgressChanged(SeekBar arg0, int arg1,
							boolean arg2) {
						if (arg2) {
							int max = audioManager
									.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
							audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
									(int) (max * arg1 / (arg0.getMax() * 1f)),
									0);
						}
					}
				});
	}

	@Override
	protected void onResume() {

		super.onResume();
	}

	@Override
	protected void onDestroy() {
		audioManager.unregisterMediaButtonEventReceiver(mediaButtonComponent); 
		super.onDestroy();
		try {
			seekBarSound.setEnabled(false);
			m_Handler.removeMessages(UPDATE_SEEKBAR);
			sound1.stopPlay();
		} catch (Exception e) {
			Log.v("TAG_ACTIVITY", "onDestroy ERROR");
		}

	}

	Handler m_Handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == UPDATE_SEEKBAR) {				
				int progress = (int) sound1.getCurrentPercantage();
				if (progress < 0)
					progress = 0;
				seekBarSound.setProgress(progress);
				textViewSoundInfo.setText(sound1.getInfoText());
				m_Handler.sendEmptyMessageDelayed(UPDATE_SEEKBAR, 100);

			}
		}
	};

	public class MediaButonListener extends BroadcastReceiver {
		@Override
		public void onReceive(Context arg0, Intent arg1) {
			Log.v("TAG_ACTIVITY", "MediaButonListener");
		}
	}

}